﻿using HrEFCore;
using HrEFCore.Models;
using HrService;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Text;

namespace HrUI.Controllers
{
	public class MCController : Controller
	{
		private readonly MyDbContext myDbContext;
		private readonly MCService mCService;
		private readonly HFService hFService;
		private readonly CFTKService cFTKService;
		private readonly CMKService cMKService;
		private readonly CMService cMService;

		public MCController(MyDbContext myDbContext,MCService mCService,HFService hFService,
			CFTKService cFTKService,CMKService cMKService,CMService cMService)
        {
			this.myDbContext = myDbContext;
			this.mCService = mCService;
			this.hFService = hFService;
			this.cFTKService = cFTKService;
			this.cMKService = cMKService;
			this.cMService = cMService;
		}
		public async Task xllb()//下拉列表
		{
			List<CFFK> list = await cFTKService.CFFKSelectAsync();
			SelectList selectListItems = new SelectList(list, "FirstKindId", "FirstKindName");
			ViewBag.cffk = selectListItems;//一级

			List<CFSK> list2 = await cFTKService.CFSKSelectAsync();
			SelectList selectListItems1 = new SelectList(list2, "SecondKindId", "SecondKindName");
			ViewBag.cfsk = selectListItems1;//二级

			List<CFTK> list3 = await cFTKService.SelectCFTKAsync();
			SelectList selectListItems2 = new SelectList(list3, "ThirdKindId", "ThirdKindName");
			ViewBag.cftk = selectListItems2;//三级

			List<CMK> list4 = await cMKService.InquireAll();
			SelectList selectListItems3 = new SelectList(list4, "MajorKindId", "MajorKindName");
			ViewBag.cmk = selectListItems3;//职位分类

			List<SS> list16 = await hFService.SSXLAsync();
			SelectList selectListItems15 = new SelectList(list16, "StandardId", "StandardName");
			ViewBag.xc = selectListItems15;//薪酬
		}
        public IActionResult Index()
		{
			return View();
		}
		public async Task<IActionResult> Index2(FenYe2<MC> mc)
		{
			mc = await mCService.SelectMCByFYAsync(mc);
			return Json(mc);
		}
		public async Task<IActionResult> Add(short id)//调动登记
		{
			string uname = HttpContext.Session.GetString("UTrueName");
			ViewBag.uname = uname;
			HF hf= await hFService.SelectHFById(id);
			ViewData.Model = hf;
			await xllb();
			return View();
		}
		public async Task<IActionResult> GetSSNameById(string salayid)//新薪酬下拉改变事件
		{
			SS ss=await mCService.SelectByIdFromSS(salayid);
			return Json(ss);
		}
		public async Task<IActionResult> MCAdd(MC mc)//添加
		{
			await mCService.DDHFAsync(mc.HumanId);
			CFFK newcffk = await cFTKService.CFFKByIdAsync(mc.NewFirstKindId);
			mc.NewFirstKindName = newcffk.FirstKindName;//新一机构

			CFSK newsecond = await cFTKService.CFSKByIdAsync (mc.NewSecondKindId);
			mc.NewSecondKindName= newsecond.SecondKindName;//新二

			CFTK newcftk = await hFService.cftkById(mc.NewThirdKindId);
			mc.NewThirdkindName = newcftk.ThirdKindName;//新三

			CMK newcmk = await hFService.SelectCMKById(mc.NewMajorKindId);
			mc.NewMajorKindName = newcmk.MajorKindName;//新职位分类

			CM newcm = await hFService.SelectCMById(mc.NewMajorId);
			mc.NewMajorName = newcm.MajorName;//新职位名称

			SS newss = await mCService.SelectByIdFromSS(mc.NewSalaryStandardId);
			mc.NewSalaryStandardName = newss.StandardName;//新薪酬

			mc.CheckReason = "";//审核通过意见
			mc.CheckStatus = 0;//复核状态0
			mc.Checker = "";//复核人空
			mc.CheckTime = DateTime.Now;//复核时间
			int row=await mCService.MCAddAsync(mc);

			HF hf = new HF();
			hf.HumanId= mc.HumanId;
			hf.FirstKindId = mc.NewFirstKindId;
			hf.FirstKindName = mc.NewFirstKindName;
			hf.SecondKindId = mc.NewSecondKindId;
			hf.SecondKindName = mc.NewSecondKindName;
			hf.ThirdKindId = mc.NewThirdKindId;
			hf.ThirdKindName = mc.NewThirdkindName;
			hf.HumanMajorKindId = mc.NewMajorKindId;
			hf.HumanMajorKindName = mc.NewMajorKindName;
			hf.HumanMajorId = mc.NewMajorId;
			hf.HunmaMajorName = mc.NewMajorName;
			hf.SalaryStandardId = mc.NewSalaryStandardId;
			hf.SalaryStandardName = mc.NewSalaryStandardName;
			hf.SalarySum = mc.NewSalarySum;
			int result = await mCService.HFUpdate(hf,mc.HumanId);

			if(row > 0 && result > 0)
			{
				return RedirectToAction("Index","MC");
			}
			else
			{
				return RedirectToAction("Index", "MC");
			}
		}
		public async Task<IActionResult> SHMCIndex(short id)//审核根据ID查询界面
		{
			string uname = HttpContext.Session.GetString("UTrueName");
			ViewBag.uname = uname;
			MC mc = await mCService.SelectMCById(id);
			ViewData.Model = mc;
			await xllb();
			return View();
		}
		public async Task<IActionResult> SHPass(MC mc)//审核通过
		{
			
			int row = await mCService.SHPass(mc);
			if(row > 0)
			{
				return RedirectToAction("Index");
			}
			else
			{
				return RedirectToAction("SHMCIndex", new {id=mc.MchId});
			}
		}
		public IActionResult Select()
		{
			return View();
		}
		public async Task<IActionResult> SelectByName(MC mc,DateTime startDate,DateTime endDate)//条件查询
		{

			if (startDate != DateTime.MinValue)
			{
				HttpContext.Session.SetString("newstartDate", startDate.ToString());
			}
			else
			{
				HttpContext.Session.SetString("newstartDate", "");
			}
			if (endDate != DateTime.MinValue)
			{
				HttpContext.Session.SetString("newendDate", endDate.ToString());
			}
			else
			{
				HttpContext.Session.SetString("newendDate", "");
			}
			if (mc.NewMajorKindId == null)
			{
				HttpContext.Session.SetString("newHumanMajorKindName", "");
			}
			else
			{
				CMK cmk = await hFService.SelectCMKById(mc.NewMajorKindId);
				mc.NewMajorKindName = cmk.MajorKindName;//职位分类名称
				HttpContext.Session.SetString("newHumanMajorKindName", mc.NewMajorKindName);
			}
			if (mc.NewMajorId == null)
			{
				HttpContext.Session.SetString("newHunmaMajorName", "");
			}
			else
			{
				CM cm = await hFService.SelectCMById(mc.NewMajorId);
				mc.NewMajorName = cm.MajorName;//职位名称	
				HttpContext.Session.SetString("newHunmaMajorName", mc.NewMajorName);
			}
			if (mc.NewFirstKindId == null)
			{
				HttpContext.Session.SetString("newFirstKindName", "");
			}
			else
			{
				CFFK cffk = await cFTKService.CFFKByIdAsync(mc.NewFirstKindId);
				mc.NewFirstKindName = cffk.FirstKindName;//一级
				HttpContext.Session.SetString("newFirstKindName", mc.NewFirstKindName);
			}
			if (mc.NewSecondKindId == null)
			{
				HttpContext.Session.SetString("newSecondKindName", "");
			}
			else
			{
				CFSK cfsk = await cFTKService.CFSKByIdAsync(mc.NewSecondKindId);
				mc.NewSecondKindName = cfsk.SecondKindName;//二级
				HttpContext.Session.SetString("newSecondKindName", mc.NewSecondKindName);
			}
			if (mc.NewThirdKindId != null)
			{
				CFTK cftk = await hFService.cftkById(mc.NewThirdKindId);
				mc.NewThirdkindName = cftk.ThirdKindName;//三级
				HttpContext.Session.SetString("newThirdKindName", mc.NewThirdkindName);
			}
			else
			{
				HttpContext.Session.SetString("newThirdKindName", "");
			}
			return View();
		}

		public async Task<List<JiLian>> GetMajorKindName()//级联
		{
			List<CMK> cmk = await cMKService.InquireAll();
			List<JiLian> jl = new List<JiLian>();
			foreach (CMK item in cmk)
			{
				JiLian jiLian = new JiLian()
				{
					value = item.MajorKindId,
					label = item.MajorKindName,
					children = await GetMajorName(item.MajorKindId)
				};
				jl.Add(jiLian);
			}
			return jl;
		}
		public async Task<List<JiLian>> GetMajorName(string id)//级联
		{
			List<CM> cm = await mCService.CMLDAsync(id);
			List<JiLian> jl = new List<JiLian>();
			foreach (var item in cm)
			{
				JiLian jiLian = new JiLian()
				{
					value = item.MajorId,
					label = item.MajorName
				};
				jl.Add(jiLian);
			}
			return jl;
		}

		public async Task<IActionResult> SelectMCFYByName(int currentPage, int pageSize)//模糊查询
		{
			string start = HttpContext.Session.GetString("newstartDate");
			string end = HttpContext.Session.GetString("newendDate");
			string firstName = HttpContext.Session.GetString("newFirstKindName");
			string secondtName = HttpContext.Session.GetString("newSecondKindName");
			string thirdName = HttpContext.Session.GetString("newThirdKindName");
			string MajorKindName = HttpContext.Session.GetString("newHumanMajorKindName");
			string MajorName = HttpContext.Session.GetString("newHunmaMajorName");
			StringBuilder where = new StringBuilder();
			if (start != "" && end != "")
			{
				where.Append($" and RegistTime between '{start}' and '{end}'");
			}
			else if (start != "" && end == "")
			{
				where.Append($" and RegistTime > '{start}'");
			}
			else if (start == "" && end != "")
			{
				where.Append($" and RegistTime < '{end}'");
			}
			else
			{
				where.Append($" ");
			}
			if (firstName == null)
			{
				where.Append($" and NewFirstKindName like '%%'");
			}
			else
			{
				where.Append($" and NewFirstKindName like '%{firstName}%'");
			}
			if (secondtName == null)
			{
				where.Append($" and NewSecondKindName like '%%'");
			}
			else
			{
				where.Append($" and NewSecondKindName like '%{secondtName}%'");
			}
			if (thirdName == null)
			{
				where.Append($" and NewThirdkindName like '%%'");
			}
			else
			{
				where.Append($" and NewThirdkindName like '%{thirdName}%'");
			}
			if (MajorKindName == null)
			{
				where.Append($" and NewMajorKindName like '%%'");
			}
			else
			{
				where.Append($" and NewMajorKindName like '%{MajorKindName}%'");
			}
			if (MajorName == null)
			{
				where.Append($" and NewMajorName like '%%'");
			}
			else
			{
				where.Append($" and NewMajorName like '%{MajorName}%'");
			}
			FenYe2<MC> fy = await mCService.SelectFYMC(currentPage, pageSize, where.ToString());
			return Json(fy);
		}
		public async Task<IActionResult> SelectXXLB(short id)//查询详细
		{
			MC mc = await mCService.SelectMCById(id);
			ViewData.Model = mc;
			return View();
		}
	}
}
